Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48345][SQL] Check variable declarations #47404

Closed

Conversation

momcilomrk-db
Copy link
Contributor

What changes were proposed in this pull request?

Checking wether variable declaration is only at the beginning of the BEGIN END block.

Why are the changes needed?

SQL standard states that the variables can be declared only immediately after BEGIN.

Does this PR introduce any user-facing change?

Users will get an error if they try to declare variable in the scope that is not started with BEGIN and ended with END or if the declarations are not immediately after BEGIN.

How was this patch tested?

Tests are in SqlScriptingParserSuite. There are 2 tests for now, if declarations are correctly written and if declarations are not written immediately after BEGIN. There is a TODO to write the test if declaration is located in the scope that is not BEGIN END.

Was this patch authored or co-authored using generative AI tooling?

No

@momcilomrk-db
Copy link
Contributor Author

momcilomrk-db commented Jul 18, 2024

@davidm-db please review


val candidates = if (allowVarDeclare) {
compoundStatements.dropWhile {
case SingleStatement(_: CreateVariable) => true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated question: within SQL scripting, shall we only allow to create single-part-name variables? e.g. should CREATE VARIABLE system.session.name be disallowed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreement with Serge is that we should be able to reference session variables at all times using system.session.<varName>. Also, local variables can be accessed by their name directly, but also using <labelName>.<varName> to allow access to all local variables in case some of them have been shadowed.

Though, what I explained is a resolution part not the creation. For the creation, looking at the code and documentation, we should allow the same thing as well:
image

I will check with Serge once again, just to confirm, but wouldn't block this PR on it because we are preparing a POC for variables anyways which is supposed to handle all local variable related stuff and we will resolve all such cases there, if that's fine by you!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for my curiosity. If SQL scripting only allows creation of local variable, then we can even fail earlier in the parser if the CREATE VARIABLE uses more than one name part.

@cloud-fan
Copy link
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in 239d77b Jul 24, 2024
ilicmarkodb pushed a commit to ilicmarkodb/spark that referenced this pull request Jul 29, 2024
### What changes were proposed in this pull request?

Checking wether variable declaration is only at the beginning of the BEGIN END block.

### Why are the changes needed?

SQL standard states that the variables can be declared only immediately after BEGIN.

### Does this PR introduce _any_ user-facing change?

Users will get an error if they try to declare variable in the scope that is not started with BEGIN and ended with END or if the declarations are not immediately after BEGIN.

### How was this patch tested?

Tests are in SqlScriptingParserSuite. There are 2 tests for now, if declarations are correctly written and if declarations are not written immediately after BEGIN. There is a TODO to write the test if declaration is located in the scope that is not BEGIN END.

### Was this patch authored or co-authored using generative AI tooling?

No

Closes apache#47404 from momcilomrk-db/check_variable_declarations.

Authored-by: Momcilo Mrkaic <momcilo.mrkaic@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
@momcilomrk-db momcilomrk-db changed the title [SPARK-48338][SQL] Check variable declarations [SPARK-48345][SQL] Check variable declarations Aug 6, 2024
fusheng-rd pushed a commit to fusheng-rd/spark that referenced this pull request Aug 6, 2024
### What changes were proposed in this pull request?

Checking wether variable declaration is only at the beginning of the BEGIN END block.

### Why are the changes needed?

SQL standard states that the variables can be declared only immediately after BEGIN.

### Does this PR introduce _any_ user-facing change?

Users will get an error if they try to declare variable in the scope that is not started with BEGIN and ended with END or if the declarations are not immediately after BEGIN.

### How was this patch tested?

Tests are in SqlScriptingParserSuite. There are 2 tests for now, if declarations are correctly written and if declarations are not written immediately after BEGIN. There is a TODO to write the test if declaration is located in the scope that is not BEGIN END.

### Was this patch authored or co-authored using generative AI tooling?

No

Closes apache#47404 from momcilomrk-db/check_variable_declarations.

Authored-by: Momcilo Mrkaic <momcilo.mrkaic@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
attilapiros pushed a commit to attilapiros/spark that referenced this pull request Oct 4, 2024
### What changes were proposed in this pull request?

Checking wether variable declaration is only at the beginning of the BEGIN END block.

### Why are the changes needed?

SQL standard states that the variables can be declared only immediately after BEGIN.

### Does this PR introduce _any_ user-facing change?

Users will get an error if they try to declare variable in the scope that is not started with BEGIN and ended with END or if the declarations are not immediately after BEGIN.

### How was this patch tested?

Tests are in SqlScriptingParserSuite. There are 2 tests for now, if declarations are correctly written and if declarations are not written immediately after BEGIN. There is a TODO to write the test if declaration is located in the scope that is not BEGIN END.

### Was this patch authored or co-authored using generative AI tooling?

No

Closes apache#47404 from momcilomrk-db/check_variable_declarations.

Authored-by: Momcilo Mrkaic <momcilo.mrkaic@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants